// pages/ticket/ticket-details/ticket-details.js
var httpUtil = require('../../../utils/httpUtil.js')
var uiUtil = require('../../../utils/uiUtil.js')
const app = getApp()
var that;
var time=1000;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    mchHome: null, //商家信息
    cardDetail: null, //会员卡详情
    chargeStrategy: null, //商家充值规则
    thatIndex:-1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that=this;
    that.ticketDetails(options.id);
    // that.memActivitys(options.mchId);
    // that.searchMchHome(options.mchId);
    that.setData({
      options: options
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow:function(){
    if (that.data.cardDetail){
      httpUtil.PostRequest("detail", {
        mCardId: that.data.cardDetail.id
      }, {
        success(res) {
          if (res.balance == that.data.cardDetail.balance) {
            setTimeout(function () {
              time += 1000;
              if(time<20000){
                that.onShow();
              }
            }, time)
          } else {
            that.setData({
              cardDetail: res
            })
            var pages = getCurrentPages();
            var lastPage = pages[0];
            var previousPage = pages[pages.length-2];
            if (lastPage.route == "pages/index/navigationbar/navigationbar") {
              let id = lastPage.data.userInfo.memberCardList[0].id;
              if (id == that.data.cardDetail.id) {
                lastPage.handleGetUserInfo();
              }
            }
            if (previousPage.route =="pages/ticket/ticket-lists/ticket-lists"){
              let balance = "ticketLists.memberCardList[" + that.data.options.index +"].balance";
              previousPage.setData({
                [balance]: res.balance
              })
            }
          }
        }
      })
    }
  },

  /**
   * 商家首页查询
   */
  searchMchHome(mchId) {
    httpUtil.PostRequest("searchMchHome", {
      mchId: mchId
    }, {
      success(res) {
        console.log(res)
        that.setData({
          mchHome: res
        });
      },
      fail(code, res) {
        uiUtil.showToast(res);
      },
      complete() { }
    })
  },

  /**
   * 充值选择金额
   */
  bindThatChange:function(e){
    let index = e.currentTarget.dataset.index;
    that.setData({
      thatIndex:index
    })
  },

  /**
   * 提示已关闭充值
   */
  hintFalse:function(){
    uiUtil.showToast("商家已关闭会员充值");
  },

  /**
   * 提示选择充值金额
   */
  hintCharge: function (e) {
    uiUtil.showToast("请选择充值金额");
  },

  /**
   * 跳转到余额明细页面
   */
  balanceDetail(e){
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/ticket/ticket-money/ticket-money?id=' + id
    })
  },

  /**
   * 获取商户充值规则
   */
  memActivitys(mchId) {
    httpUtil.PostRequest("memActivitys", {
      mchId: mchId
    }, {
        success(res) {
          that.setData({
            chargeStrategy: res
          })
        },
        fail(code, res) {
          uiUtil.showToast(res);
        },
        complete() { }
      })
  },

  /**
   * 获取会员卡详细信息
   */
  ticketDetails(mCardId){
    httpUtil.PostRequest("detail", {
      mCardId: mCardId
    }, {
      success(res) {
        that.setData({
          cardDetail:res
        })
      },
      fail(code, res) {
        uiUtil.showToast(res);
      },
      complete() {}
    })
  }
})